﻿#region 版权信息
// ------------------------------------------------------------------------------
// Copyright: (c) 2029  成都A+有限公司 - Amm
// 项目名称：Amm.Admin
// 文件名称：ApiExceptionFilterAttribute.cs
// 创建时间：2020101716:03
// 更改时间：2020101716:03
//  ------------------------------------------------------------------------------
#endregion

using Colorful.Furniture.Wrappers;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;
using System;
using System.Net;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Authorization;
using Volo.Abp.Validation;

namespace Colorful.Furniture
{
    /// <summary>
    ///  api接口错误异常拦截
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
    public class ApiExceptionFilterAttribute : ExceptionFilterAttribute
    {
        /// <summary>
        ///  异常
        /// </summary>
        /// <param name="context"></param>
        public override async Task OnExceptionAsync(ExceptionContext context)
        {
            var result = new StandardResponseWrapper<object>
            {
                Code = 600,
                Success = false,
                Message = context.Exception.Message
            };

            switch (context.Exception)
            {
                //自定义异常
                case UserFriendlyException _:

                    result.Message = context.Exception.Message;
                    //将对应的错误码返回
                    if (context.Exception is UserFriendlyException userFriendlyException)
                    {
                        if (userFriendlyException.Code != default)
                            result.Data = userFriendlyException.Code;
                    }

                    break;

                //错误请求异常
                case AbpValidationException _:
                    result.Message = context.Exception.Message;
                    result.Code = 400;
                    break;
                //未授权 || 未认证
                case AbpAuthorizationException authorizationException:
                    var authorizationCode = authorizationException.Code.To<int>();
                    result.Code = authorizationCode;
                    result.Message = authorizationCode == (int)HttpStatusCode.Unauthorized ? "未认证" : "未授权";
                    break;
                default:
                    result.Message = "服务在开小差，请稍候重试";
                    result.Code = 500;
                    break;
            }

            //处理HTTP响应内容
            context.HttpContext.Response.Headers.Add("Colorful-Furniture-CustomException", "1");
            context.HttpContext.Response.StatusCode = result.Code;
            context.HttpContext.Response.ContentType = "application/json;charset=utf-8";
            await context.HttpContext.Response.WriteAsync(JsonConvert.SerializeObject(result));

            context.ExceptionHandled = true;
        }
        

    }

    
}